﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Commonality.Model;
using Commonality.Model.Tray;

namespace Commonality.DAL.WZS_SFCS
{
    /// <summary>
    /// 单位产品数据结构
    /// </summary>
    public struct ProductCellData
    {
        public string usn;
        public string[] usnInfo;
        public string[] bin;
    }

    public static class SFCSInterface
    {
        private static WebService SFCS;

        public static void SFCSInterfaceInit()
        {
            SFCS = new WebService();
        }

        /// <summary>
        /// 获取一个SN的基础数据
        /// </summary>
        /// <param name="USN">扫入的SN</param>
        /// <param name="infoNameValues">SN指向的所有关注信息集合</param>
        /// <returns>OK or error message</returns>
        public static string GetKeyInfoFromView(string USN,ref clsInfoNameValue[] infoNameValues)
        {
            //infoNameValues = { };
            //原本接口
            return SFCS.GetKeyInfoFromView(USN, "PO", "15", ref infoNameValues);
            //调试接口
            #region 调试接口
            infoNameValues[0].InfoValue = USN;
            infoNameValues[1].InfoValue = "PO|1234|4321|X1098";
            infoNameValues[3].InfoValue = "N|OK|NONPMP|NA";
            return "OK";
            #endregion
        }

        /// <summary>
        /// 获取一个SN的基础数据
        /// </summary>
        /// <param name="USN">扫入的SN</param>
        /// <param name="infoNameValues">SN指向的所有关注信息集合</param>
        /// <returns>OK or error message</returns>
        public static string GetKeyInfoFromView(string USN, ref string Values)
        {
            string str = "";
            try
            {
                clsInfoNameValue[] infoNameValues = { };
                //原本接口
                str = SFCS.GetKeyInfoFromView(USN, "PO", "15", ref infoNameValues);
                //if (infoNameValues.Length != 1)
                //{
                //    WebService SFCS = new WebService();
                //    Thread.Sleep(10);
                //    str = SFCS.GetKeyInfoFromView(USN, "PO", "15", ref infoNameValues);
                //}
                Values = infoNameValues[0].InfoValue.ToString();
            return str;
            }
            catch (Exception ex)
            {

                return str;
            }
            
        }

        /// <summary>
        /// cartonid方法
        /// </summary>
        /// <param name="usnBunch">SN串</param>
        /// <param name="workstation">工作站名(纬创自定义)</param>
        /// <param name="line">线别(纬创自定义)</param>
        /// <param name="userID">作业员ID</param>
        /// <returns></returns>
        public static string AutoPackingforFATP(string usnBunch,string workstation,string line,string userID)
        {
            return SFCS.AutoPackingforFATP(usnBunch, "PO", workstation, line, userID);
        }
        /// <summary>
        /// 自动称重方法
        /// </summary>
        /// <param name="USN">carton内任意SN</param>
        /// <param name="CartonWeight">称得的重量</param>
        /// <param name="Workstation">工作站名(纬创自定义)</param>
        /// <param name="Line">线别(纬创自定义)</param>
        /// <param name="UserID">作业员ID</param>
        /// <returns></returns>
        public static string AutoWeight(string USN, string CartonWeight, string Workstation, string Line, string UserID)
        {
            return SFCS.AutoWeight(USN, CartonWeight,"PP", Workstation,Line,UserID);
        }
        /// <summary>
        /// 包装箱匹配方法
        /// </summary>
        /// <param name="CartonID">cartonID</param>
        /// <param name="CartonCPN">包装箱号</param>
        /// <returns></returns>
        public static string AutoCompare(string CartonID, string CartonCPN)
        {
            return SFCS.AutoCompare(CartonID, CartonCPN, "PO");
        }
    }
    public static class SFCSDataDispose
    {
        public static void SFCSDataDisposeInit()
        {
            //SFCSControl_ConsistencyAsyn = SFCSControl_Consistency;
            SFCSControl_ConsistencyAsynFlag = false;
        }

        /// <summary>
        /// 入口准入判断
        /// 1.PO站
        /// </summary>
        /// <param name="USN"></param>
        /// <param name="Perm">过站OK</param>
        /// <param name="productCell">dataList</param>
        public static string SFCSControl_Admittance(ref string USN,ref bool Perm,ref string Message,ref ProductCellData productCell)
        {
            ProductCellData ProductCell = new ProductCellData() { usn = "", usnInfo = new string[25], bin = new string[10] };
            string str = GetKeyInfoFromView_DataDispose(USN, ref ProductCell.usn,ref ProductCell.usnInfo,ref ProductCell.bin);

            //if (ProductCell.usn == USN)
            //{
            //    if (ProductCell.usnInfo[5] == "PO")
            //    {
            //        Perm = true;
            //    }
            //    else
            //    {
            //        Perm = false;
            //        Message = "站点错误：" + ProductCell.usnInfo[5];                  
            //    }
            //}
            //else
            //{
            //    Perm = false;
            //    Message = "USN：匹配错误";
            //}

            try
            {
                if (ProductCell.usnInfo[6] == "PO")
                {
                    Perm = true;
                    Message = "过站正确";
                }
                else
                {
                    Perm = true;
                    Message = "站点错误：" + ProductCell.usnInfo[6];
                }
            }
            catch (Exception)
            {

                Perm = false;
                Message = str;
            }


            productCell = ProductCell;
            return str;
        }
        

        /// <summary>
        /// Tray内匹配
        /// 1.同tray内两片产品匹配是同类型产品
        /// 2.同类型
        /// </summary>
        /// <param name="USN1">SN1</param>
        /// <param name="USN2">SN1</param>
        /// <param name="Cons">匹配结果</param>
        /// <param name="Message">错误信息</param>
        /// <param name="productCell1">单元数据</param>
        /// <param name="productCell2">单元数据</param>
        /// <returns></returns>
        public static string[] SFCSControl_Consistency(ref string USN1,ref string USN2,ref bool Cons,ref string Message,ref ProductCellData productCell1, ref ProductCellData productCell2)
        {
            string[] str = new string[2];
            ProductCellData ProductCell1 = new ProductCellData() { usn = USN1, usnInfo = new string[25] , bin = new string[10] };
            ProductCellData ProductCell2 = new ProductCellData() { usn = USN2, usnInfo = new string[25], bin = new string[10] };


            str[0] = GetKeyInfoFromView_DataDispose(USN1, ref ProductCell1.usn, ref ProductCell1.usnInfo, ref ProductCell1.bin);
            str[1] = GetKeyInfoFromView_DataDispose(USN2, ref ProductCell2.usn, ref ProductCell2.usnInfo, ref ProductCell2.bin);
            //提出对比项
            string[] infoa = new string[11] {   ProductCell1.usnInfo[0],
                                                 ProductCell1.usnInfo[3],
                                                ProductCell1.usnInfo[4],
                                                ProductCell1.usnInfo[10],
                                                ProductCell1.usnInfo[12],
                                                ProductCell1.usnInfo[14],
                                                ProductCell1.usnInfo[15],
                                                ProductCell1.usnInfo[16],
                                                ProductCell1.usnInfo[17],
                                                ProductCell1.usnInfo[20],
                                                ProductCell1.usnInfo[21]
            };

            string[] infob = new string[11] {   ProductCell2.usnInfo[0],
                                                 ProductCell2.usnInfo[3],
                                                ProductCell2.usnInfo[4],
                                                ProductCell2.usnInfo[10],
                                                ProductCell2.usnInfo[12],
                                                ProductCell2.usnInfo[14],
                                                ProductCell2.usnInfo[15],
                                                ProductCell2.usnInfo[16],
                                                ProductCell2.usnInfo[17],
                                                ProductCell2.usnInfo[20],
                                                ProductCell2.usnInfo[21]
            };


            if (Enumerable.SequenceEqual(infoa, infob))
            {
                Cons = true;
                Message = "OK";
                productCell1.usn = USN1;
                productCell1.usnInfo = ProductCell1.usnInfo;
                productCell2.usn = USN2;
                productCell2.usnInfo = ProductCell2.usnInfo;
                return str;
            }
            else
            {
                Cons = false;
                Message = "Info：匹配不一致";
                productCell1.usn = USN1;
                productCell1.usnInfo = ProductCell1.usnInfo;
                productCell2.usn = USN2;
                productCell2.usnInfo = ProductCell2.usnInfo;
                return str;
            }
            
        }
        /// <summary>
        /// Tray内匹配
        /// 1.同tray内两片产品匹配是同类型产品
        /// 2.同类型
        /// </summary>
        /// <param name="USN1">SN1</param>
        /// <param name="USN2">SN1</param>
        /// <param name="Cons">匹配结果</param>
        /// <param name="Message">错误信息</param>
        /// <param name="productCell1">单元数据</param>
        /// <param name="productCell2">单元数据</param>
        /// <returns></returns>
        //public static Tuple<bool, ProductCellData, ProductCellData> SFCSControl_Consistency(string USN1,string USN2)
        //{

        //    //string[] str = new string[2];
        //    //ProductCellData ProductCell1 = new ProductCellData() { usn = USN1, usnInfo = new string[20], bin = new string[10] };
        //    //ProductCellData ProductCell2 = new ProductCellData() { usn = USN2, usnInfo = new string[20], bin = new string[10] };


        //    //str[0] = GetKeyInfoFromView_DataDispose(USN1, ref ProductCell1.usn, ref ProductCell1.usnInfo, ref ProductCell1.bin);
        //    //str[1] = GetKeyInfoFromView_DataDispose(USN2, ref ProductCell2.usn, ref ProductCell2.usnInfo, ref ProductCell2.bin);

        //    //Log4Net.MsgInfo(ProductCell1.usnInfo[0] + "/" + ProductCell1.usnInfo[1]);
        //    //Log4Net.MsgInfo(ProductCell2.usnInfo[0] + "/" + ProductCell2.usnInfo[1]);
        //    ////对比产品信息

        //    ////提出对比项
        //    //string[] infoa = new string[4] { ProductCell1.usnInfo[0], ProductCell1.usnInfo[3], ProductCell1.usnInfo[4], ProductCell1.usnInfo[5] };
        //    //string[] infob = new string[4] { ProductCell2.usnInfo[0], ProductCell2.usnInfo[3], ProductCell2.usnInfo[4], ProductCell2.usnInfo[5] };

        //    //Tuple<bool, ProductCellData, ProductCellData> Ret = new Tuple<bool, ProductCellData, ProductCellData>(Enumerable.SequenceEqual(infoa, infob), ProductCell1, ProductCell2);

        //    //return Ret;
        //}



        /// <summary>
        /// Tray内匹配-异步
        /// </summary>
        public static Func<string, string, Tuple<bool, ProductCellData, ProductCellData>> SFCSControl_ConsistencyAsyn;
        /// <summary>
        /// 异步访问中
        /// </summary>
        public static bool SFCSControl_ConsistencyAsynFlag;

        #region ProductCellData方法
        /// <summary>
        /// 从产品DataList中获取Moedel,本地方法未访问SFC
        /// </summary>
        /// <param name="productCell"></param>
        /// <param name="trayModel"></param>
        public static void ProductCellData_Model(ProductCellData productCell, ref TrayModel trayModel)
        {
            //TODU:Model类型分析
            if (Tray.X01.Contains(productCell.usnInfo[3]))
            {
                trayModel = TrayModel.X01;
            }
            else if (Tray.X03.Contains(productCell.usnInfo[3]))
            {
                trayModel = TrayModel.X03;
            }
            else if (Tray.X05.Contains(productCell.usnInfo[3]))
            {
                trayModel = TrayModel.X05;
            }
            else if (Tray.X08.Contains(productCell.usnInfo[3]))
            {
                trayModel = TrayModel.X08;
            }
            else if (Tray.X13.Contains(productCell.usnInfo[3]))
            {
                trayModel = TrayModel.X13;
            }
            else if (Tray.X15.Contains(productCell.usnInfo[3]))
            {
                trayModel = TrayModel.X15;
            }
            else if (Tray.X1776.Contains(productCell.usnInfo[3]))
            {
                trayModel = TrayModel.X1776;
            }
            else if (Tray.X1777.Contains(productCell.usnInfo[3]))
            {
                trayModel = TrayModel.X1777;
            }
            else if (Tray.X1778.Contains(productCell.usnInfo[3]))
            {
                trayModel = TrayModel.X1778;
            }
            else if (Tray.X19.Contains(productCell.usnInfo[3]))
            {
                trayModel = TrayModel.X19;
            }
            else
            {
                trayModel = TrayModel.Unknown;
            }


        }
        #endregion

        #region 私有方法
        public static string GetKeyInfoFromView_DataDispose(string USN,ref string usn,ref string[] usnInfo,ref string[] bin)
        {
            //clsInfoNameValue[] infoNameValues = { };
            //infoNameValues[0].InfoName = "likin";
            //infoNameValues[0].InfoValue = "likin";

            //string str = SFCSInterface.GetKeyInfoFromView(USN, ref infoNameValues);
            // try
            // {
            //     if (infoNameValues[0].InfoValue == null)
            //     {
            //         SFCSInterface.GetKeyInfoFromView(USN, ref infoNameValues);
            //         if (infoNameValues[0].InfoValue == null)
            //         {

            //         }
            //     }
            // }
            // catch (Exception)
            // {
            //     SFCSInterface.GetKeyInfoFromView(USN, ref infoNameValues);

            // }

            // try
            // {
            //     usnInfo = infoNameValues[0].InfoValue.Split(new char[] { '|' });
            // }
            // catch (Exception)
            // {

            //     infoNameValues[0].InfoValue = "0|0|0|0|0|0|0|0|0|0|1";
            // }
            //Log4Net.MsgInfo(infoNameValues[0].InfoValue);

            string Values = "";
            string str = SFCSInterface.GetKeyInfoFromView(USN, ref Values);
            try
            {
                string[] Info = Values.Split(new char[] { '|' });

                for (int i = 0; i < usnInfo.Length; i++)
                {
                    usnInfo[i] = "";
                }
                for (int i = 0; i < Info.Length; i++)
                {
                    usnInfo[i] = Info[i];
                }

                if (usnInfo[10] == "")
                {
                    usnInfo[10] = "1";      //X13，X5,X8默认无此项，这里默认值1
                }
            }
            catch (Exception e)
            {

            }





            return str;
        }
        #endregion
    }


}
